﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using AidBrella.Entities;
using Aidbrella.Model;

namespace AidBrella.WCFServiceWebRole
{
    public static class Functions
    {
        internal static User GetCurrentUser()
        {
            if (ServiceSecurityContext.Current.IsAnonymous)
                return null;

            if (ServiceSecurityContext.Current.PrimaryIdentity.IsAuthenticated)
            {
                var userName = ServiceSecurityContext.Current.PrimaryIdentity.Name;

                using (var ctx = new AidBrellaModelContainer())
                {
                    return ctx.UserSet.Where(u => u.UserName == userName).FirstOrDefault();
                }
            }

            return null;
        }
    }
}